#include "include.h"
#include "func.h"
#include "func_idle.h"
#include "api_spiflash_ex.h"
#include "func_exspiflash_music.h"

#if FUNC_IDLE_EN
#define MAX_FUNC_IDLE_TIMEOUT 30
#define MAX_MICON_IDLE_TIMEOUT 5*60

u16 func_timeout_cnt;
u16 max_timeout_time;
u8 sfx_play=0;
u8 long_wakeup=0;
u8 mic_play=0;
AT(.text.func.idle.table)
const u8 sfx_idle_light_mode_table[]=
{
	LIGHT_SEQUENCE_7,
	LIGHT_SEQUENCE_8,
	LIGHT_SEQUENCE_9,
	LIGHT_SEQUENCE_10,
	LIGHT_SEQUENCE_11,
	LIGHT_SEQUENCE_12,
	LIGHT_SEQUENCE_13,
	LIGHT_SEQUENCE_2,
	LIGHT_SEQUENCE_20,
};
AT(.text.func.idle.table)
const u8 sfx_idle_table[]=
{
	16,17,18,19,20,21,9,8,22,23
};
AT(.text.func.music.table)
const u8 mic_idle_table[]=
{
	15,14
};



AT(.text.func.idle)
void func_ilde_timeout(void)
{
	if(sfx_play)
	{
		return;
	}
	if(mic_play)
	{
		return;
	}
	func_timeout_cnt++;
	amp_speaker_mute();
	printf("idle cnt:%d\n",func_timeout_cnt);
	if(func_timeout_cnt>=max_timeout_time)
	{
		music_control(MUSIC_MSG_STOP);
		func_cb.sta=FUNC_PWROFF;
		func_timeout_cnt=0;
	}
}
AT(.text.func.idle)
void func_ilde_reset(void)
{
	func_timeout_cnt=0;
}
AT(.text.bsp.idle)
void func_idle_mp3_res_play(u32 addr, u32 len)
{
    if (len == 0) {
        return;
    }
	mp3_res_play(addr, len);
}
u8 bakup_vol;
AT(.text.func.idle)
void func_idle_mic_onoff_ctl(void)
{
	printf("mic_onoff:%d\n",sys_cb.mic_onoff);
	#if SYS_KARAOK_EN
	if(sys_cb.mic_onoff)
	{
		sys_cb.mic_onoff=0;
		max_timeout_time=MAX_FUNC_IDLE_TIMEOUT;
		//karaok_mic_mute();
		bsp_karaok_exit(AUDIO_PATH_KARAOK);
		bsp_set_light_mode(LIGHT_SEQUENCE_14);
	}
	else
	{
		if(sys_cb.vbat_low)
		{
			return;
		}
		sys_cb.mic_onoff=1;
		max_timeout_time=MAX_MICON_IDLE_TIMEOUT;
		//karaok_mic_unmute();
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		bsp_set_light_mode(LIGHT_SEQUENCE_1);
	}
	amp_speaker_unmute();
	mic_play=1;
	exspifalsh_music_num_kick(mic_idle_table[sys_cb.mic_onoff]);	
	
	#endif
}
extern void test_color(void);

AT(.text.func.idle)
void func_ilde_lower_power_check(void)
{
	#if SYS_KARAOK_EN
	if(sys_cb.mic_onoff&&sys_cb.karaok_init)
	 {
	 	u8 mic_off;
	 	mic_off=bsp_get_mic_vol_offset();
		if(sys_cb.mic_vol_offset!=mic_off)
		{
			sys_cb.mic_vol_offset=mic_off;
			bsp_karaok_set_mic_volume();
		}
		if ((sys_cb.vbat <=2700) ||(adc_cb.vrtc_val>=0x39f))
		 {
		 	sys_cb.vbat_low=1;
			bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 }			 	
	 }
	#endif
}

AT(.text.func.idle)
void func_idle_sfx_ctl(void)
{
	//test_color();
	//return;
	printf("sfx_index:%d\n",sys_cb.sfx_index);
	sys_cb.sfx_index++;
	if(sys_cb.sfx_index>9)
	{
		sys_cb.sfx_index=1;
	}
	if(sfx_play)
	{
		music_control(MUSIC_MSG_STOP);
	}
	amp_speaker_unmute();
	exspifalsh_music_num_kick(sfx_idle_table[sys_cb.sfx_index-1]);
	bsp_set_sfx_mode(sfx_idle_light_mode_table[sys_cb.sfx_index-1]);
	sfx_play=1;
	func_timeout_cnt=0;

}
AT(.text.func.idle)
void func_idle_set_sfx_off(void)
{
	sys_cb.sfx_index=9;
	bsp_set_sfx_mode(sfx_idle_light_mode_table[sys_cb.sfx_index-1]);
}

AT(.text.func.idle)
void func_idle_sfx_status(void)
{
	if (get_music_dec_sta() == MUSIC_STOP)
	{
		if(sfx_play)
		{
			sfx_play=0;
			music_control(MUSIC_MSG_STOP);
			amp_speaker_mute();
		}
		if(mic_play)
		{
			printf("mic end!!\n");
			mic_play=0;
			music_control(MUSIC_MSG_STOP);
			if(sys_cb.sfx_index!=0)
			{
				bsp_set_sfx_mode(sfx_idle_light_mode_table[sys_cb.sfx_index-1]);
			}
			else
			{
				bsp_set_light_mode(LIGHT_SEQUENCE_2);
			}
		}
	}
}
AT(.text.func.idle)
void func_idle_process(void)
{
    func_process();
	func_idle_sfx_status();
}
void func_power_on_play(void)
{
	bsp_set_light_mode(LIGHT_SEQUENCE_1);
	mp3_res_play(RES_BUF_EN_POWERON_MP3, RES_LEN_EN_POWERON_MP3);
	#if !FUNC_NO_SPEECH
	delay_5ms(100);	
	param_poweron_index_read();
	if(sys_cb.poweron_tts_index==0)
	{
		sys_cb.poweron_tts_index=1;
		bsp_set_light_mode(LIGHT_SEQUENCE_3A);
		mp3_res_play(RES_BUF_EN_ON_NEXT1_MP3, RES_LEN_EN_ON_NEXT1_MP3);
	}
	else
	{
		sys_cb.poweron_tts_index=0;
		bsp_set_light_mode(LIGHT_SEQUENCE_3B);
		mp3_res_play(RES_BUF_EN_NO_NEXT2_MP3, RES_LEN_EN_NO_NEXT2_MP3);
	}
	param_poweron_index_write();
	#endif
	mic_bias_trim();
}
static void func_idle_enter(void)
{
#if PLUGIN_FUNC_IDLE_ENTER_CHECK
    if (!plugin_func_idle_enter_check()) {  //可以处理开机, 判断是否要停在idle模式, 等待设备插入的方案.
        return;
    }
#endif
	func_power_on_play();
	exspiflash_init();
	func_cb.mp3_res_play = NULL;//func_exspifalsh_mp3_res_play;
    //register_spi_read_function(spiflash1_read);
	bsp_change_volume(0);
	
	#if SYS_KARAOK_EN	
	if(sys_cb.mic_onoff)
	{
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
	}
	#endif
	bsp_change_volume(MUSIC_VOL);
    led_idle();
	bsp_set_light_mode(LIGHT_SEQUENCE_2);
	func_timeout_cnt=0;
	max_timeout_time=MAX_FUNC_IDLE_TIMEOUT;

}

static void func_idle_exit(void)
{
	func_timeout_cnt=0;
	//register_spi_read_function(NULL);  //恢复内部SPI读接口.
	//music_control(MUSIC_MSG_STOP);
    func_cb.last = FUNC_IDLE;
}
AT(.text.func.idle)
u8 check_power_key(void)
{
	u8 key_cnt=0;
	u16 msg;
	while (1)
	{
	    WDT_CLR();
	    delay_ms(20);
		msg = msg_dequeue();
		printf("msg:0x%x\n",msg);
		if(msg==KEY_POWER||msg==KL_POWER||msg==KH_POWER)
		{
			key_cnt=10;
			break;
		}
		key_cnt++;
		if(key_cnt>6)
		{
			break;
		}
	}
	if(key_cnt>=10)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

AT(.text.func.idle)
void func_idle(void)
{
    printf("%s\n", __func__);
	if(check_power_key()==0)
	{
		printf("key long!!\n");
		long_wakeup=1;
	}
    func_idle_enter();

    while (func_cb.sta == FUNC_IDLE) {
        func_idle_process();
        func_idle_message(msg_dequeue());
        func_idle_display();
    }

    func_idle_exit();
}

#endif
